package cn.crc.pjblog.pages;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.tapestry5.ComponentResources;
import org.apache.tapestry5.annotations.Retain;
import org.apache.tapestry5.beaneditor.BeanModel;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.services.BeanModelSource;

import cn.crc.model.BlogContent;
import cn.crc.model.imp.IPublicDAO;
import cn.crc.pjblog.bean.BeanDefaultRow;
import cn.crc.spring.SpringBean;

public class DefaultGrid {
	
	@Inject
	@SpringBean("IPublicDAO")
	private IPublicDAO pdao;
	
	@Inject
	private BeanModelSource bms;
	
	@Retain
	private BeanModel model;
	
	@Inject 
	private ComponentResources resources;
	
	private BeanDefaultRow row;
	
	
	public void onActivate(){
		
		this.model = this.bms.create(BeanDefaultRow.class, true, this.resources);
//		this.model.add("logTitle").sortable(false).label("标题");
		
	}
	
	
	
	
	public List getDataItems(){
//		String hsql = "from  BlogContent";
		String hsql = "select LogTitle,LogPosttime from  BlogContent";
		
		List list = this.pdao.getDataByHSQL(hsql);
		
		System.out.println("list.size()="+list.size());
		
		BeanDefaultRow bdr = null;
		List<BeanDefaultRow> listbdr = new ArrayList<BeanDefaultRow>(0);
//		if (list.get(0) instanceof BlogContent){
//			System.out.println("BlogContent ");
//		}else if (list.get(0) instanceof Object[])
//		{
//			System.out.println("object[]==");
//		}
//		else
//		{
//			System.out.println("else instanceof");
//		}
		 Iterator it = list.iterator();
		 
		 while (it.hasNext()){
			 bdr= new BeanDefaultRow();
			 Object[] results = (Object[])it.next();	
			 
//			 System.out.println("results.size=="+results.length);
//			 System.out.println("results[0]="+results[0]);
//			 System.out.println("results[1]="+results[1]);
			 
			 if (results[0] != null ){
				 bdr.setLogTitle(results[0].toString());
			 }
			 if (results[1] != null ){
				 bdr.setLogPostTime(results[1].toString());
			 }
			 
			 listbdr.add(bdr);
		 }
		

		
		return listbdr;
	}



	/**
	 * @return the pdao
	 */
	public IPublicDAO getPdao() {
		return pdao;
	}



	/**
	 * @param pdao the pdao to set
	 */
	public void setPdao(IPublicDAO pdao) {
		this.pdao = pdao;
	}




	/**
	 * @return the bms
	 */
	public BeanModelSource getBms() {
		return this.bms;
	}




	/**
	 * @return the model
	 */
	public BeanModel getModel() {
		return this.model;
	}




	/**
	 * @param bms the bms to set
	 */
	public void setBms(BeanModelSource bms) {
		this.bms = bms;
	}




	/**
	 * @param model the model to set
	 */
	public void setModel(BeanModel model) {
		this.model = model;
	}
//	




	/**
	 * @return the resources
	 */
	public ComponentResources getResources() {
		return resources;
	}




	/**
	 * @param resources the resources to set
	 */
	public void setResources(ComponentResources resources) {
		this.resources = resources;
	}




	/**
	 * @return the row
	 */
	public BeanDefaultRow getRow() {
		return row;
	}




	/**
	 * @param row the row to set
	 */
	public void setRow(BeanDefaultRow row) {
		this.row = row;
	}
	
}
